package com.jinxin.platform.cdfaceacs.task;

import com.jinxin.platform.cdfaceacs.bean.CdfaceacsDevice;
import com.jinxin.platform.cdfaceacs.bean.CdzxkacsDevice;
import com.jinxin.platform.cdfaceacs.rpc.SysUserRpc;
import com.jinxin.platform.cdfaceacs.service.IAcsDeviceService;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Description: bccah
 * Created by 34759 on 2019/8/16 15:57
 */
@EnableScheduling
@Configuration
@Component
@Slf4j
public class DeviceTask {
    @Autowired
    private SysUserRpc sysUserRpc;
    @Autowired
    private IFaceAcsDeviceService acsDeviceService;
    @Autowired
    private IAcsDeviceService acsDeviceService2;
    /**
     * 定时更新设备状态 每隔30秒更新一次哦
     */
//    @Scheduled(cron = "0/30 * * * * ?  ")
//    public void updateDeviceStatus2(){
//        List<CdzxkacsDevice> list = acsDeviceService2.list(new HashMap());
//        for(CdzxkacsDevice device : list) {
//            String serialNum = device.getSerialNum();
//            int deviceStatus = sysUserRpc.getDeviceStatus(serialNum, device.getProductCode());
//            String statusStr = deviceStatus == 0 ? "在线" : "未知";
//            log.info("获取设备：Mac(" + device.getDeviceMac()+")的在线状态是：" + statusStr);
//            device.setStatus(deviceStatus);
//            acsDeviceService2.saveOrUpdate(device);
//        }
//    }
    /**
     * 定时更新设备状态 每隔30秒更新一次哦
     */
    @Scheduled(cron = "0/30 * * * * ?  ")
    public void updateDeviceStatus(){
        List<CdfaceacsDevice> list = acsDeviceService.list(new HashMap());
        for(CdfaceacsDevice device : list) {
            String serialNum = device.getSerialNum();
            String deviceKey = sysUserRpc.getDeviceKey(serialNum, device.getProductCode());
            if(!StringUtils.isEmpty(deviceKey) && StringUtils.isEmpty(device.getDeviceKey())){
                device.setDeviceKey(deviceKey);
                acsDeviceService.saveOrUpdate(device);
            }
            int deviceStatus = !StringUtils.isEmpty(deviceKey) ? 0 : 1;
            String statusStr = !StringUtils.isEmpty(deviceKey) ? "在线" : "未知";
            log.info("获取设备：Mac(" + device.getDeviceMac()+")的在线状态是：" + statusStr);
            device.setStatus(deviceStatus);
            acsDeviceService.saveOrUpdate(device);
        }
    }

}
